﻿using SqlDao;
using SqlDaoDemo;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DotDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        #region property

        public string DbFile
        {
            get
            {
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data.db");
            }
        }

        private SqliteHelper sqliteHelper;

        public SqliteHelper MHelper
        {
            get
            {
                DbHelper.isDebug = true;
                sqliteHelper ??= SqliteHelper.Instance;
                return sqliteHelper;
            }
        }

        #endregion
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// connect if not exist Create database
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void ConnBtn_Click(object sender, RoutedEventArgs e)
        {
            var res = MHelper.CreateDatabase(DbFile);
            Debug.WriteLine(res);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string dataPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data");
            if (!Directory.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }
        }
        /// <summary>
        /// create table and insert data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InitBtn_Click(object sender, RoutedEventArgs e)
        {
            string table = "car_info";
            bool exist = MHelper.ExistTable("main", table);
            if (!exist)
            {
                string dir = AppDomain.CurrentDomain.BaseDirectory;
                string sqlfile = dir.Substring(0, dir.LastIndexOf("bin"));
                string sql = File.ReadAllText(System.IO.Path.Combine(sqlfile, "sql", "car_info.sql"));

                int rows = MHelper.ExecuteNonQuery(sql);
                Debug.WriteLine("ROWS:" + rows);
            }
        }

        private void SelectBtn_Click(object sender, RoutedEventArgs e)
        {
            string sql = "SELECT * from car_info;";    
            List<CarInfo> carInfos = MHelper.Select<CarInfo>(sql);
            carInfos.ForEach(carInfo =>
            {
                Debug.WriteLine(carInfo.carNumber);
            });
        }

        private void updateBtn_Click(object sender, RoutedEventArgs e)
        {
            string sql = "Update car_info set car_number = '北A88888' where car_number = '贵C77777';";

            int res = MHelper.Update(sql);

            Debug.WriteLine("Update " + (res > 0 ? "OK" : "ERROR"));

        }
    }
}